package cn.huiyunche.bms.web.controller;

import cn.huiyunche.bms.backend.bo.AdjustBO;
import cn.huiyunche.bms.backend.query.SServiceOrderConditions;
import cn.huiyunche.bms.backend.query.WayBillDetailConditions;
import cn.huiyunche.bms.backend.service.OrderService;
import cn.huiyunche.bms.backend.service.WayBillService;
import cn.huiyunche.bms.web.query.WayBillDetailQuery;
import cn.huiyunche.bms.web.vo.AdjustVO;
import cn.huiyunche.bms.web.vo.ReceivableBillVO;
import cn.huiyunche.commons.domain.PageVo;
import cn.huiyunche.commons.domain.ResultDTO;
import cn.huiyunche.commons.exception.BusinessException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;

/**
 * Created by felix on 2017/8/2 9527.
 */

@Controller
@RequestMapping("/adjust")
public class AdjustController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdjustController.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private WayBillService wayBillService;

    /**
     * 查询订单价格列表
     * @param pageVo
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/order-list", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> orderList(PageVo pageVo, ReceivableBillVO vo) throws Exception {
        LOGGER.info("AdjustController.orderList param : {}", pageVo, vo);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        try {
            SServiceOrderConditions conditions = new SServiceOrderConditions();
            BeanUtils.copyProperties(vo,conditions);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            if (vo.getBillDateStartString() != null)
                conditions.setCreateTimeStart(format.parse(vo.getBillDateStartString()));
            if (vo.getBillDateEndString() != null)
                conditions.setCreateTimeEnd(format.parse(vo.getBillDateEndString()));
            result.setData(orderService.queryOrderByConditions(pageVo, conditions));
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AdjustController.orderList error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     * 订单价格调整
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update-cost", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> updateCost(AdjustVO vo, BindingResult br) throws Exception {
        LOGGER.info("AdjustController.updateCost param : {}",  vo);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "修改成功");
        try {
            AdjustBO bo = new AdjustBO();
            BeanUtils.copyProperties(vo, bo);
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String username = (String)session.getAttribute("username");
            bo.setCreateUser(username);
            result.setData(orderService.updateCost(bo, br));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            LOGGER.error("AdjustController.updateCost error: {}", e);
//            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     * 查询运单价格列表
     * @param pageVo
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/waybill-list", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> wayBillList(PageVo pageVo, WayBillDetailQuery vo) throws Exception {
        LOGGER.info("AdjustController.wayBillList param : {}", pageVo, vo);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "查询成功");
        try {
            WayBillDetailConditions conditions = new WayBillDetailConditions();
            BeanUtils.copyProperties(vo,conditions);
            conditions.setWayBillCodes(vo.getWaybillCode());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            if (vo.getCreateTimeStart() != null)
                conditions.setCreateTimeStart(format.parse(vo.getCreateTimeStart()));
            if (vo.getCreateTimeEnd() != null)
                conditions.setCreateTimeEnd(format.parse(vo.getCreateTimeEnd()));
            result.setData(wayBillService.queryWayBillByConditions(pageVo, conditions));
        } catch (Exception e) {
            result.setSuccess(false);
            LOGGER.error("AdjustController.wayBillList error: {}", e);
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     * 运单价格调整
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update-waybill", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultDTO<Object> updateWayBillCost(AdjustVO vo, BindingResult br) throws Exception {
        LOGGER.info("AdjustController.updateWayBillCost param : {}",  vo);
        ResultDTO<Object> result = new ResultDTO<>(true, null, "修改成功");
        try {
            AdjustBO bo = new AdjustBO();
            BeanUtils.copyProperties(vo, bo);
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String username = (String)session.getAttribute("username");
            bo.setCreateUser(username);
            result.setData(wayBillService.updateCost(bo, br));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            LOGGER.error("AdjustController.updateWayBillCost error: {}", e);
        }
        return result;
    }
}
